
#include "MCU_include.h"

bit Ad_Samp_Flag;
_ADC_FLAG 	ADC_FLAG;
_ADC_FLAG1	ADC_FLAG1;

vu16	V_Ad_Value, I_Ad_Value[2] = {0};
vu16	u16_PowerMea, u16_PowerMeaSum = 0;
vu8		PowerMeaCnt = 0 ;
vu16	 Ki=0;
vu16 	V_Ac[V_AVG_NUM] = {0}, V_AcStable = 0;
vu16	I_Ac[I_AVG_NUM] = {0}, I_Ac_Changed = 0, I_ChangedTH = 0, I_AcStable = 0;
vu8		Ad_Ch_Sel,AD_Temp_IGBT, AD_TEMP_Coil, AD_Temp_PAN , AD_Temp_PAN_Quality;
u16		I_AD_Change; 
vu8		MoveInCnt = 0, MoveOutCnt = 0, AvrgTimesCnt = 0;
vu32	Ac_Val = 0;
vu8		PowerAddCnt = 0;
vu16	V_AcH = 0, V_AcL = 0;


u8 AD_Iac_Buf[8+1]={0};
u8 AD_Vac_Buf[8+1]={0};
u8 Moving_AVG_Filter(u8 Input_Data, u8 * p_filter_buf, u8 Length) ;

void ADC_Init()
{
	_sadc0 = 0b00110000;	//START ADBZ ADCEN ADRFS SACS3 SACS2 SACS1 SACS0 
	_sadc1 = 0b00001100;	//External channel, reference voltage VDD, clock Fsys/16=1MHz
}
u16 AD_Convert(u8 ADC_Channel) // Put interrupt
{

	vu16 TempAD_Value;	
    switch (Ad_Ch_Sel) //
    {

    //--------------
    case get_voltage_ADvalue:
		//	//Measure the voltage AD value	
			_sadc1 &= 0b00011111;
		if((ADC_Channel >= 0)&&(ADC_Channel <= 12))
		{
			/* clear ADC channel */
			_sadc0 &= 0b11110000;
			/* selection external ADC channel */
			_sadc0 |= ADC_Channel;      
		}
		else
		{
			/* selection internal ADC channel */
			_sadc1 |= ADC_Channel;      
		}
		ADC_AUTO_Converted_DIS_times();
        break;
    //--------------
    case get_current_ADvalue:
    	//Measure the current AD value
    		_sadc1 &= 0b00011111;
		if((ADC_Channel >= 0)&&(ADC_Channel <= 12))
		{
			/* clear ADC channel */
			_sadc0 &= 0b11110000;
			/* selection external ADC channel */
			_sadc0 |= ADC_Channel;      
		}
		else
		{
			/* selection internal ADC channel */
			_sadc1 |= ADC_Channel;    
			//	 _pa7 = ~_pa7;  
		}   
    
//       	_lebc = 0b00000001;		//AADCST AUTOADC1 AUTOADC0 LEB4 LEB3 LEB2 LEB1 LEB0
//   		_sadc2 = 0b11000011;
//		_aadcst = 1;
		ADC_AUTO_Converted_DIS_times();

        break;
    //--------------
    case get_IGBTtemp_ADvalue:
    //Measure the temperature AD value of IGBT
    	_sadc1 &= 0b00011111;
		if((ADC_Channel >= 0)&&(ADC_Channel <= 12))
		{
			/* clear ADC channel */
			_sadc0 &= 0b11110000;
			/* selection external ADC channel */
			_sadc0 |= ADC_Channel;      
		}
		else
		{
			/* selection internal ADC channel */
			_sadc1 |= ADC_Channel;      
		}
		ADC_AUTO_Converted_DIS_times();
    
        break;
    //--------------
    case get_TOPtemp_ADvalue:
    //Measure the AD value of the pot bottom temperature
    	_sadc1 &= 0b00011111;
		if((ADC_Channel >= 0)&&(ADC_Channel <= 12))
		{
			/* clear ADC channel */
			_sadc0 &= 0b11110000;
			/* selection external ADC channel */
			_sadc0 |= ADC_Channel;      
		}
		else
		{
			/* selection internal ADC channel */
			_sadc1 |= ADC_Channel;      
		}
		ADC_AUTO_Converted_DIS_times();
    
        break;
    //--------------
 
    default:
        break;
    }
	ADC_Start();				//
	_nop();
    while(_adbz);				//
    TempAD_Value= (_sadoh << 8) | _sadol;	

    return TempAD_Value;
    
}




u32	V_AdSum = 0, I_AdSum = 0;
void Get_Power_Temp()
{
			static	u8	Ad_Samp_Cnt=0;
			
			static  u16 V_Ad_Min, V_Ad_Max, I_Ad_Min, I_Ad_Max;

			static vu16  Vad_Buff_Temp16=0;
			static vu8  Vad_Buff_Temp8=0,V_buff_debounce;
			static vu16  IAD_Buff_Temp16=0;
			static vu8  IAD_Buff_Temp8=0;
			
        	if(Ad_Samp_Flag == 1)				
         	{
				Ad_Samp_Flag = 0;
				V_Ad_Value	 = V_Ad_Value>>4;		
				I_Ad_Value[0] = I_Ad_Value[0]>>4;	
	
					//**************************	
				if(!Ad_Samp_Cnt)
				{
					V_Ad_Min = V_Ad_Value;
					V_Ad_Max = V_Ad_Value;
					V_AdSum = 0;		
					I_Ad_Min = I_Ad_Value[0];
					I_Ad_Max = I_Ad_Value[0];
					I_AdSum = 0;
			//******************************Eliminate peak fluctuations*****************		
				}
				else
				{
	         		if(V_Ad_Value > V_Ad_Max)
	         			V_Ad_Max = V_Ad_Value;					
	         		if(V_Ad_Value < V_Ad_Min)
	         			V_Ad_Min = V_Ad_Value;					
	         		
	         		if(I_Ad_Value[0] > I_Ad_Max)
	         			I_Ad_Max = I_Ad_Value[0];			
	         		if(I_Ad_Value[0] < I_Ad_Min)
	         			I_Ad_Min = I_Ad_Value[0];				         		
					
	        //*********************************************************	
				}
         		V_AdSum=V_AdSum + V_Ad_Value;					
         		I_AdSum=I_AdSum + I_Ad_Value[0];					 
         		
         		Ad_Samp_Cnt++;											
         		if(Ad_Ch_Sel >= 2 )	//			IGBT and furnace surface temperature can be collected once each
         		{	Ad_Samp_Cnt = Ad_Sample_Times;}
				else
				{
					/*V_Ad_Value = 0;
		   			I_Ad_Value[0] = 0;*/
         		}
         		if(Ad_Samp_Cnt >= Ad_Sample_Times)		
         		{
					Ad_Samp_Cnt = 0;
				
         			V_AdSum-=V_Ad_Max;				
         			V_AdSum-=V_Ad_Min;					
         			I_AdSum-=I_Ad_Max;				
         			I_AdSum-=I_Ad_Min;					
         			
         			switch(Ad_Ch_Sel)
         			{
         				case get_voltage_ADvalue:
         				/**************************** Voltage data processing *****************************/		
							
								Vad_Buff_Temp16 = V_AdSum / 8;    							
								Vad_Buff_Temp8=(vu8) Vad_Buff_Temp16;
								Vad_Buff_Temp8=Moving_AVG_Filter(Vad_Buff_Temp8, AD_Vac_Buf, (8+1)) ;
								ADC_V_result=(vu16) Vad_Buff_Temp8;
						
							if(ADC_V_result - V_buff_debounce >1)
							{
								V_buff_debounce	=	ADC_V_result;							
							}
							else
							{
								ADC_V_result =	V_buff_debounce ;
							}	
							
				        /************************** ************************/				         		

			         			_ctm2af=0;
		         				break;
		         	   	case get_current_ADvalue:
							/************************** Current data processing ****************************/					         									

								IAD_Buff_Temp16 = I_AdSum /8;							
								IAD_Buff_Temp8=(vu8) IAD_Buff_Temp16;
								IAD_Buff_Temp8=Moving_AVG_Filter(IAD_Buff_Temp8, AD_Iac_Buf, (8+1)) ;
								ADC_I_result=(vu16) IAD_Buff_Temp8;		
				
							
								_ctm2af = 0;
								break;																				
		         		case get_IGBTtemp_ADvalue:

								AD_Temp_IGBT = V_Ad_Value;						
								_ctm2af = 0;
								break;		
		         		case get_TOPtemp_ADvalue:
								AD_Temp_PAN = V_Ad_Value;						
								break;	

								break;
         			}
         			
         			Ad_Ch_Sel++;							
         			if(Ad_Ch_Sel > 4)						
         			{
         				Ad_Ch_Sel=0;
//         				CTM2_OFF();	
         				if(IH_Status_APP.flagBit.error!=ERROR_OCP && IH_Status_APP.flagBit.error!=ERROR_PHASE && IH_Status_APP.flagBit.error!=ERROR_SURGE && IH_Status_APP.flagBit.error!=ERROR_I2C)
         				IH_Status_APP.flagBit.error = 	NO_ERROR_CODE;	
         				//ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F = 0;
         				VoltageTemperatureDet();	
         					//************************
						   	if(u16_PowerSet >= 2500)
							{
								Ki = K_Current_3 ;
							}
							else if(u16_PowerSet >= 1600)
							{
								Ki = K_Current_2 ;
							}
							else
							{
								Ki = K_Current_1 ;
							}
					
         			
         				 PowerCalculation_Function(K_Voltage, Ki, B_Voltage , B_Current);
         				//************************												
						u16_PowerMea = R_PowerIV;				//Read power value	
						//********************************			
	         			ADC_FLAG.flagBit.Get_Power_Fg = 1;
	         			
	         		}
    		  
				}
         		else	_ctm2af=0;				
         	}	
}


	







/******************* End of  file ***************/